//
//  AppDelegate.swift
//  WinsonWeibo
//
//  Created by Administrator on 2017/4/14.
//  Copyright © 2017年 Winson. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()

        // 设置额外的信息
        setupAdditions()

        loadAPPInfo()

        
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

// MARK: - 额外的信息设置
extension AppDelegate {

    fileprivate func setupAdditions() {

        // 设置SVProgressHUD最小消失时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)

        // 设置SVProgressHUD的样式
        SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.dark)

        // 设置网络请求状态的指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true

    

        // iOS 10以上的版本
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert, UNAuthorizationOptions.badge, UNAuthorizationOptions.carPlay,UNAuthorizationOptions.sound])
            { (success, error) in
                print("授权" + (success ? "成功" : "失败"))
            }
        } else {
            // iOS 10 以下的版本
            // Fallback on earlier versions
            /// 取得用户授权显示通知【上方的提示条／声音／badgeNumber】
            let notifySettings = UIUserNotificationSettings(types: [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound], categories: nil)

            UIApplication.shared.registerUserNotificationSettings(notifySettings)
        }
    }
}


// MARK: - 从服务器加载应用程序
extension AppDelegate {

    /// 1. 模拟异步
    func loadAPPInfo() {
        DispatchQueue.global().sync {
            // 1. url
            let url = Bundle.main.path(forResource: "main.json", ofType: nil)

            // 2. data
            let data = NSData(contentsOfFile: url!)

            // 3. 写入沙盒
            // 获取沙盒路径
            var filePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
            filePath = filePath + "/main.json"
            data?.write(toFile: filePath, atomically: true)
            print("app 信息加载完毕\(filePath)")
        }
    }
}

